let _requestConfig;
export function registerModels(requestConfig) {
	_requestConfig = requestConfig;
}

function _appendBaseUrl(originUrl) {
	return `${_requestConfig.baseUrl}${originUrl}`;
}

// 定义RequestParam类，用于封装请求参数
export class RequestParam {
	constructor(param) {
		// 解构赋值，从param中获取参数
		const {
			useBaseUrl = true,
				url,
				data,
				method = 'GET',
				header,
				dataType = 'json',
				responseType = 'text',
				timeout = 6000,
				signal
		} = param || {}
		// 将参数赋值给实例属性
		this.useBaseUrl = useBaseUrl;
		this.url = useBaseUrl ? _appendBaseUrl(url) : url;
		this.data = data;
		this.method = method;
		this._method = method;
		this.header = header;
		this.dataType = dataType;
		this.responseType = responseType;
		this.timeout = timeout;
		this.signal = signal;
	}
	get method() {
		return this._method ? this._method.toUpperCase() : 'GET';
	}
	set method(value) {
		this._method = value;
	}
}

// 定义UploadParam类，用于封装上传参数
export class UploadParam {
	constructor(param) {
		// 解构赋值，从param中获取参数
		const {
			useBaseUrl = true,
				url,
				filePath,
				name,
				header,
				formData,
				timeout = 6000,
				signal
		} = param || {}
		// 将参数赋值给实例属性
		this.useBaseUrl = useBaseUrl;
		this.url = useBaseUrl ? _appendBaseUrl(url) : url;
		this.filePath = filePath;
		this.name = name;
		this.formData = formData;
		this.header = header;
		this.timeout = timeout;
		this.signal = signal;
	}
}

// 定义DownloadParam类，用于封装下载参数
export class DownloadParam {
	constructor(param) {
		// 解构赋值，从param中获取参数
		const {
			useBaseUrl = true,
				url,
				header,
				timeout = 6000,
				filePath,
				signal
		} = param || {}
		// 将参数赋值给实例属性
		this.useBaseUrl = useBaseUrl;
		this.url = useBaseUrl ? _appendBaseUrl(url) : url;
		this.header = header;
		this.timeout = timeout;
		this.filePath = filePath;
		this.signal = signal;
	}
}

export default {
	registerModels
}